﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EzDesk.Attendance.Model
{
    public class AttendanceEvaluationInfo
    {
        public AttendanceEvaluationInfo()
        {
            // default values
            IsLate = false;
            IsEarlyLeave = false;
            IsExtraWork = false;
            IsLeaveForWork = false;
            IsCompleteAttendance = false;
            IsFragmentaryAttendance = true;
            IsLeaveWholeDay = false;
            IsAbsence = true;
            LateMiniutes = 0;
            EarlyMiniutes = 0;
            ExtraWorkTime = 0;

        }

        #region 考勤状况描述

        public DayInfo DayInfo { get; set; }

        public AttendanceInfo OnDutyAction { get; set; }
        public AttendanceInfo OffDutyAction { get; set; }

        /// <summary>
        /// 假期状况
        /// </summary>
        public DayLeaveInfo LeaveInfo { get; set; }

        /// <summary>
        /// 是否迟到
        /// </summary>
        public bool IsLate { get; set; }
        /// <summary>
        /// 迟到时间
        /// </summary>
        public int LateMiniutes { get; set; }

        /// <summary>
        /// 是否早退
        /// </summary>
        public bool IsEarlyLeave { get; set; }

        /// <summary>
        /// 早退时间
        /// </summary>
        public int EarlyMiniutes { get; set; }

        /// <summary>
        /// 是否整日有假期
        /// </summary>
        public bool IsLeaveWholeDay { get; set; }

        /// <summary>
        /// 是否加班
        /// </summary>
        public bool IsExtraWork { get; set; }
        /// <summary>
        /// 加班时间
        /// </summary>
        public int ExtraWorkTime { get; set; }


        /// <summary>
        /// 是否一天在外工作
        /// </summary>
        public bool IsLeaveForWork { get; set; }

        /// <summary>
        /// 是否完整考勤
        /// </summary>
        public bool IsCompleteAttendance { get; set; }

        /// <summary>
        /// 是否为残缺的记录
        /// </summary>
        public bool IsFragmentaryAttendance { get; set; }

        /// <summary>
        /// 是否被记录缺勤
        /// </summary>
        public bool IsAbsence { get; set; }

        /// <summary>
        /// 全天调休使用情况
        /// </summary>
        public int ShiftWorkHours
        {
            get
            {
                if (CustomizedShiftWork != null && CustomizedShiftWork.AuditedUserId.HasValue)
                {
                    return CustomizedShiftWork.CustomizedShitWorkMiniute;
                }
                else
                {
                    if (IsAbsence)
                    {
                        return 80; 
                    }
                    else
                    {
                        return ReplacedEarlyHours + ReplacedLateHours - ReplacedExtraWorkTime;
                    }
                }
            }
        }


        public CustomizedShiftWork CustomizedShiftWork { get; set; }
        #endregion



        private int GetReplaymentHours(int originTime)
        {
            if (originTime == 0) return 0;
            int replacement = (originTime / 30 + 1) * 5;
            return Math.Min(replacement, 80);
        }


        /// <summary>
        /// 早退消耗调休时间
        /// </summary>
        public int ReplacedEarlyHours
        {
            get
            {
                return GetReplaymentHours(EarlyMiniutes);
            }
        }

        /// <summary>
        /// 迟到消耗调休时间
        /// </summary>
        public int ReplacedLateHours
        {
            get { return GetReplaymentHours(LateMiniutes); }
        }


        /// <summary>
        /// 加班获得调休时间
        /// </summary>
        public int ReplacedExtraWorkTime
        {
            get
            {
                return GetReplaymentHours(ExtraWorkTime);
            }
        }
    }
}
